package com.ruoyi.tj.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/dict")
public class DictController {

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @PostMapping("/items")
    @ResponseBody
    public AjaxResult items(@RequestBody SysDictData dictData) {
        List<SysDictData> sysDictData = dictDataService.selectDictDataList(dictData);
        sysDictData.sort(Comparator.comparing(SysDictData::getDictSort));
        return AjaxResult.success(sysDictData);
    }

    @PostMapping("/types")
    @ResponseBody
    public AjaxResult types(@RequestBody SysDictType dictType) {
        List<SysDictType> sysDictTypes = dictTypeService.selectDictTypeList(dictType);
        sysDictTypes.sort(Comparator.comparing(SysDictType::getDictId));
        return AjaxResult.success(sysDictTypes);
    }
}
